package im.ok.back.controller;

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import im.ok.back.common.FileSuffixEnum;
import im.ok.back.common.ServerResponse;
import im.ok.back.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("pdf")
public class PDFController {

    @PostMapping("pdf2word")
    public ServerResponse pdf2word(@RequestParam("PDFFile") MultipartFile file) {
        try {
            // 保存文件
            Map<String, Object> resMap = CommonUtil.saveFile(file, FileSuffixEnum.PDF.getDesc(), FileSuffixEnum.DOC.getDesc());
            if (resMap != null && resMap.get("msg") == null) {
                // 转换
                PdfDocument pdf = new PdfDocument();
                pdf.loadFromFile(resMap.get("originalFilePath").toString());
                // 保存为Word格式
                pdf.saveToFile(resMap.get("transformFilePath").toString(), FileFormat.DOCX);
                return ServerResponse.createSuccessData("上传并转换成功！", CommonUtil.commonProcess(resMap, 0));
            } else {
                return ServerResponse.createError(resMap.get("msg").toString());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("getPdf2Word")
    public void getPdf2Word (String fileName, HttpServletResponse response) {
        CommonUtil.exportFile(fileName, FileSuffixEnum.DOC.getDesc(), response);
    }

    @PostMapping("pdf2html")
    public ServerResponse pdf2html(@RequestParam("PDFFile") MultipartFile file) {
        try {
            // 保存文件
            Map<String, Object> resMap = CommonUtil.saveFile(file, FileSuffixEnum.PDF.getDesc(), FileSuffixEnum.HTML.getDesc());
            if (resMap != null && resMap.get("msg") == null) {
                // 转换
                PdfDocument pdf = new PdfDocument();
                pdf.loadFromFile(resMap.get("originalFilePath").toString());
                // 保存为Word格式
                pdf.saveToFile(resMap.get("transformFilePath").toString(), FileFormat.HTML);
                return ServerResponse.createSuccessData("上传并转换成功！", CommonUtil.commonProcess(resMap, 1));
            } else {
                return ServerResponse.createError(resMap.get("msg").toString());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("getPdf2Html")
    public void getPdf2Html (String fileName, HttpServletResponse response) {
        CommonUtil.exportFile(fileName, FileSuffixEnum.HTML.getDesc(), response);
    }
}
